#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

#include "pcommon.h"
#include "..\game\middleware.h"
#include "zone.h"

#define MAXPRINTMSG	1024
#define MAX_CONSOLE_LINES 500

extern pboolean Parse_Config(char*);
extern pboolean g_cmd_with_gui;

/*
=============
Com_Error

it will do the apropriate things.
=============
*/
void Com_Error(char *error, ...)
{
	va_list		argptr;
	char		text[MAXPRINTMSG];

	va_start(argptr, error);
	vsprintf(text, error, argptr);
	va_end(argptr);

	MessageBox(NULL, text, "Error", 0 /* MB_OK */ );

	exit(1);
}

/*
=============
Com_Printf

Both client and server can use this, and it will output
to the apropriate place.
=============
*/
void Com_Printf (char *fmt, ...)
{
	va_list		argptr;
	char		msg[MAXPRINTMSG];
	double		dTemp = 0.0;	/* force loading floating point support */

	va_start (argptr,fmt);
	vsprintf (msg,fmt,argptr);
	va_end (argptr);

	printf("%s", msg);
}

/*
=============
Con_Init

it will do the apropriate things.
=============
*/
void Con_Init(void)
{
#ifdef __CONSOLE_ATTACHED
	int hConHandle;
	long lStdHandle;
	CONSOLE_SCREEN_BUFFER_INFO coninfo;
	FILE *fp;

	// allocate a console for this app
	AllocConsole();
	
	// set the screen buffer to be big enough to let us scroll text
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), 
		&coninfo);
	coninfo.dwSize.Y = MAX_CONSOLE_LINES;
	SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), 
		coninfo.dwSize);

	// redirect unbuffered STDOUT to the console
	lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
	fp = _fdopen( hConHandle, "w" );
	*stdout = *fp;
	setvbuf( stdout, NULL, _IONBF, 0 );

	// redirect unbuffered STDIN to the console
	lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
	fp = _fdopen( hConHandle, "r" );
	*stdin = *fp;
	setvbuf( stdin, NULL, _IONBF, 0 );

	// redirect unbuffered STDERR to the console
	lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
	fp = _fdopen( hConHandle, "w" );
	*stderr = *fp;
	setvbuf( stderr, NULL, _IONBF, 0 );
#endif
}

/*
=================
Pcommon_Init
=================
*/
void Pcommon_Init(void)
{
	/* init console */
	Con_Init();

	/* read the outer configuration to determine if we'll load GUI */
	Parse_Config("..\\Scripts\\config.txt");

	/* load middleware libraries */
	Mid_LoadLib();

	if (g_cmd_with_gui)
	{
		/* wait for user's configuration action via GUI system */
		pboolean status = GUI_Init();
		if (false == status)
		{
			PostQuitMessage(0);
			return;
		}
	}

	/* init video driver */
	if (!Vid_Init())
	{
		Com_Error("Can't init gvs.");
	}
}

/*
=================
Pcommon_Frame
=================
*/
void Pcommon_Frame(void)
{
	/* Render a frame */
	Vid_Update();
}

/*
=================
Pcommon_Shutdown
=================
*/
void Pcommon_Quit(void)
{
	Vid_Shutdown();
	Mid_FreeLib();

	/* uninit zone allocator */
	Drop_Global_Zone();
}

